package com.mtons.mblog.web.controller.learn;

import com.mtons.mblog.base.lang.Consts;
import com.mtons.mblog.base.utils.MarkdownUtils;
import com.mtons.mblog.modules.data.AccountProfile;
import com.mtons.mblog.modules.data.LearnDocVO;
import com.mtons.mblog.modules.entity.LearnPlan;
import com.mtons.mblog.modules.service.LearnContentTypeService;
import com.mtons.mblog.modules.service.LearnDocService;
import com.mtons.mblog.modules.service.LearnPlanService;
import com.mtons.mblog.web.controller.BaseController;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

import static org.apache.commons.lang3.StringUtils.isNotEmpty;

@Controller("learnDocController")
@RequestMapping("/learn/doc")
public class LearnDocController extends BaseController {

    @Resource
    private LearnDocService learnDocService;

    @Resource
    private LearnContentTypeService contentTypeService;


    @Autowired
    private LearnPlanService learnPlanService;

    @RequestMapping("/list")
    public String list(String title, ModelMap model, HttpServletRequest request) {
        String contentTypeId = ServletRequestUtils.getStringParameter(request, "docType", "");
        Pageable pageable = wrapPageable(Sort.by(Sort.Direction.DESC, "contentTypeId", "createTime"));
        Page<LearnDocVO> page = learnDocService.paging(pageable, contentTypeId, title);
        model.put("page", page);
        model.put("title", title);
        model.put("docType", contentTypeId);
        model.put("contentTypes", contentTypeService.findAll());
        return "/learn/doc/list";
    }

    /**
     * 跳转到卡片内容编辑页面
     *
     * @param id
     * @param model
     * @return
     */
    @RequestMapping(value = "/view", method = RequestMethod.GET)
    public String toUpdate(String id, ModelMap model) {
        String editor = siteOptions.getValue("editor");
        if (isNotEmpty(id)) {
            LearnDocVO view = learnDocService.get(id);
            if (StringUtils.isNoneBlank(view.getEditor())) {
                editor = view.getEditor();
            }
            model.put("view", view);
        }
        LocalDateTime ldt = LocalDateTime.now();
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd");
        String nowDate = ldt.format(dtf);
        model.put("minDate", nowDate);
        model.put("editor", editor);
        model.put("contentTypes", contentTypeService.findAll());
        return "/learn/doc/view";
    }

    /**
     * 更新文章方法
     *
     * @return
     * @author 小神灯的火焰
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public String update(LearnDocVO docVO) {
        if (docVO != null) {
            if (isNotEmpty(docVO.getId())) {
                learnDocService.update(docVO);
            } else {
                AccountProfile profile = getProfile();
                docVO.setAuthorId(profile.getId());
                learnDocService.post(docVO);
            }
        }
        return "redirect:/learn/doc/list";
    }

    @RequestMapping(value = "/show", method = RequestMethod.GET)
    public String show(String id, ModelMap model) {
        //当前id应该是计划的主键
        //通过计划查询对应的文章id信息
        LearnPlan learnPlan = learnPlanService.findById(id);
        if (learnPlan != null && StringUtils.equals(learnPlan.getRemindStatus(), Consts.COMMON_NO)) {
            learnPlanService.updateRemindStatusId(id);
        }
        LearnDocVO view = learnDocService.get(learnPlan.getDocId());
        if ("markdown".endsWith(view.getEditor())) {
            LearnDocVO learnDocVO = new LearnDocVO();
            BeanUtils.copyProperties(view, learnDocVO);
            learnDocVO.setContent(MarkdownUtils.renderMarkdown(view.getContent()));
            view = learnDocVO;
        }
        model.put("view", view);
        model.put("planId", id);
        model.put("reviewDate", DateFormatUtils.format(learnPlan.getRemindTime(), "yyyy-MM-dd HH:mm:ss"));
        String score = learnPlan.getScore();
        if (StringUtils.isNotEmpty(score)) {
            model.put("score", score);
        }
        return "learn/doc/show";
    }

    @RequestMapping(value = "/score", method = RequestMethod.POST)
    public String score(String id, String score, ModelMap model) {
        LearnPlan learnPlan = learnPlanService.findById(id);
        if (StringUtils.equals(learnPlan.getScore(), "-1")) {
            learnPlanService.updateScoreById(id, score);
        }
        return "redirect:/learn/doc/show?id=" + id;
    }
}
